<?php
/**
 * @author: xaboy<365615158@qq.com>
 * @day: 2017/11/11
 */

namespace app\__MODULE__\model;
use think\Model;


/**
 * 产品管理 model
 * Class StoreProduct
 * @package app\__MODULE__\model\store
 */
class Goods extends BaseModel
{

    /**
     * 数据表主键
     * @var string
     */
    protected $pk = 'id';

    /**
     * 模型名称
     * @var string
     */
    protected $name = 'goods';
    protected $autoWriteTimestamp = true;

    public function cate(){
        return $this->belongsTo(StoreCategory::class,'cate_id')->bind(['cate_name']);
    }

    public function getDescriptionAttr($value)
    {
        return htmlspecialchars_decode($value);
    }

    public function getSliderImageAttr($value){
        return $value ? json_decode($value,true) : [];
    }

    //编辑库存
    public static function changeStock($stock, $productId)
    {
        return self::edit(compact('stock'), ['id'=>$productId]);
    }

    //获取库存数量
    public static function getStock($productId)
    {
        return StoreProductAttrValue::where(['product_id' => $productId, 'type' => 0])->sum('stock');
    }

    //获取总销量
    public static function getSales($productId)
    {
        return StoreProductAttrValue::where(['product_id' => $productId, 'type' => 0])->sum('sales');
    }

    public static function getTierList($model = null)
    {
        if ($model === null) $model = new self();
        return $model->field('id,store_name')->where('is_del', 0)->select()->toArray();
    }

 

    /*
     * 处理二维数组排序
     * $arrays 需要处理的数组
     * $sort_key 需要处理的key名
     * $sort_order 排序方式
     * $sort_type 类型 可不填写
     */
    public static function my_sort($arrays, $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC)
    {
        if (is_array($arrays)) {
            foreach ($arrays as $array) {
                if (is_array($array)) {
                    $key_arrays[] = $array[$sort_key];
                } else {
                    return false;
                }
            }
        }
        if (isset($key_arrays)) {
            array_multisort($key_arrays, $sort_order, $sort_type, $arrays);
            return $arrays;
        }
        return false;
    }

   

    /**
     * TODO 获取某个字段值
     * @param $id
     * @param string $field
     * @return mixed
     */
    public static function getProductField($id, $field = 'store_name')
    {
        return self::where('id', $id)->value($field);
    }

        /**
     * 获取单个商品得属性unique
     * @param int $productId
     * @param int $type
     * @return bool|mixed
     */
    public static function getSingleAttrUnique(int $productId, int $id = 0, int $type = 0)
    {
        if (self::where(['id' => $productId, 'spec_type' => 1])->count()) {
            return false;
        } else {
            $unique = GoodsAttr::where(['product_id' => $id ?: $productId, 'type' => $type])->value('unique');
            return $unique ?: false;
        }
    }

    /**
     * 检查商品是否可以购买
     * @param  [int]  $productId [商品id]
     * @return boolean           
     */
        public static function isValidProduct($productId)
    {
        return self::where(['id' => $productId, 'is_del' => 0, 'is_show' => 1])->count() > 0;
    }

        /**
     * 检查商品库存
     * @param  [int] $productId [商品id]
     * @param  string $uniqueId  [属性值]
     * @return [int]            [description]
     */
        public static function getProductStock($productId, $uniqueId = '')
    {
        return $uniqueId == '' ?
            self::where('id', $productId)->value('stock') ?: 0
            : GoodsAttr::uniqueByStock($uniqueId);
    }

       /**
     * 加销量减销量
     * @param $num
     * @param $productId
     * @param string $unique
     * @return bool
     */
    public static function decProductStock($num, $productId, $unique = '')
    {
        if ($unique) {
            $res = false !== GoodsAttrValue::decProductAttrStock($productId, $unique, $num, 0);
            $res = $res && self::where('id', $productId)->dec('stock', $num)->inc('sales', $num)->update();
        } else {
            $res = false !== self::where('id', $productId)->dec('stock', $num)->inc('sales', $num)->update();
        }
        return $res;
    }

    /**
     * 减少销量,增加库存
     * @param int $num 增加库存数量
     * @param int $productId 产品id
     * @param string $unique 属性唯一值
     * @return boolean
     */
    public static function incProductStock($num, $productId, $unique = '')
    {
        $product = self::where('id', $productId)->field(['sales', 'stock'])->find();
        if (!$product) return true;
        if ($product->sales > 0) $product->sales = bcsub($product->sales, $num, 0);
        if ($product->sales < 0) $product->sales = 0;
        $res = true;
        if ($unique) {
            $res = false !== GoodsAttrValue::incProductAttrStock($productId, $unique, $num);
        }
        $product->stock = bcadd($product->stock, $num, 0);
        $res = $res && $product->save();
        return $res;
    }

    /**
     * 判断属性商品是否存在
     * @param  [type] $productId [description]
     * @param  [type] $unique    [description]
     * @return [type]            [description]
     */
    public static function issetProductUnique($productId, $unique)
    {
        $res = self::where('product_id', $productId)->find();
        if ($unique) {
            return $res && GoodsAttrValue::where('product_id', $productId)->where('unique', $unique)->count() > 0;
        } else {
            return !$res;
        }
    }
}